﻿using Smobiler.Core;
using Smobiler.Core.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using FubiManger.Controls;
using Model;

namespace FubiManger.UI
{
    partial class EditaddrForm : Smobiler.Core.Controls.MobileForm
    {
        public EditaddrForm() : base()
        {
            //This call is required by the SmobilerForm.
            InitializeComponent();
        }
        /// <summary>
        /// 商户ID
        /// </summary>
        public string merchantid { get; set; }
       /// <summary>
       /// 经度
       /// </summary>
        private string Longitude { get; set; }
        /// <summary>
        /// 纬度
        /// </summary>
        private string Latitude { get; set; }
        /// <summary>
        /// 省
        /// </summary>
        private string prov { get; set; }
        /// <summary>
        /// 区
        /// </summary>
        private string area { get; set; }
        /// <summary>
        /// 市
        /// </summary>
        private string city { get; set; }
        private void panclose_Press(object sender, EventArgs e)
        {
            this.Close();
        }

        private void pansave_Press(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(txtcity.Text))
                {
                    MessageBox.Show("请设置城市");
                }
                else if (string.IsNullOrEmpty(txtaddr.Text))
                {
                    MessageBox.Show("请设置详细地址");
                }
                else
                {
                    if (merchantDal.Updateaddr(prov, city, area, txtaddr.Text, Longitude, Latitude, merchantid) > 0)
                    {
                        Toast("地址保存成功");
                        this.Close();
                    }
                }
            }
            catch (Exception)
            {

                Toast("网络异常");
            }
        }

        private void gps1_GotLocation(object sender, GPSResultArgs e)
        {
            if (e.isError == false)
            {
                GPSResultArgs gps = e.ToGCJ02();
                Longitude = gps.Longitude.ToString();//经度
                Latitude = gps.Latitude.ToString();//纬度
                txtaddr.Text = gps.Location;//当前位置
            }
            //else
            //    MessageBox.Show(e.error);
        }

        private void panaddr_Press(object sender, EventArgs e)
        {
            gps1.GetGps();
        }
        /// <summary>
        /// 打开省、市
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pandown_Press(object sender, EventArgs e)
        {
            Regioncontrol con = new Regioncontrol();
            DialogOptions options = new DialogOptions(LayoutJustifyAlign.FlexEnd);
            options.Padding = Padding.Empty;
            this.Form.ShowDialog(con, options, (obj, args) =>
            {
                if (con.level == "3")
                {
                    prov = con.prov;//省
                    city = con.city;//市
                    area = con.area;//区
                    txtcity.Text = prov + city + area;//省市区
                }
            });
        }

        private void EditaddrForm_Load(object sender, EventArgs e)
        {
           
            ms_merchant merchant = merchantDal.Querybyid(merchantid);
            txtcity.Text = merchant.province + merchant.city + merchant.area;//省市区
            txtaddr.Text = merchant.address;//地址
            Longitude = merchant.longitude;//经度
            Latitude = merchant.latitude;//纬度
            prov = merchant.province;//省
            city = merchant.city;//市
            area = merchant.area;//区
        }

        private void btnsendaddr_Press(object sender, EventArgs e)
        {
            EditsendAddrForm frm = new EditsendAddrForm();
            frm.mercahntid = merchantid;
            this.Form.Show(frm, true);
        }
    }
}